<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>ECharts</title>
    <!--   引入 echarts.js -->
    <script src="/page/echarts.js"></script>
    <!-- 引入 jQuery.js -->
    <script src="/page/jquery-3.7.0.min.js"></script>
    <style>
        #main {
            width: 600px;
            height: 400px;
        }

        #myModal {
            display: none;
            position: fixed;
            z-index: 1;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgba(0, 0, 0, 0.5);
        }

        #modal-content {
            background-color: #fefefe;
            margin: 15% auto;
            padding: 20px;
            border: 1px solid #888;
            width: 80%;
        }

        #close {
            color: #aaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
            cursor: pointer;
        }

        #close:hover,
        #close:focus {
            color: black;
            text-decoration: none;
            cursor: pointer;
        }
    </style>
</head>
<body>
<!-- 为 ECharts 准备一个具备大小（宽高）的 DOM -->
<div id="main" style="width: 1000px;height:550px;"></div>
<div id="myModal" class="modal">
    <div id="modal-content">
        <span id="close" onclick="closeModal()">×</span>
        <p id="modalContent"></p>
    </div>
</div>
<script>
    // 假设的API地址
    const apiUrl = 'http://localhost:8080/authorReadAve';

    // 初始化ECharts实例
    const chart = echarts.init(document.getElementById('main'));

    // 获取数据
    fetch(apiUrl)
        .then(response => response.json())
        .then(data => {
            // 转换数据为ECharts所需格式
            const seriesData = data.map(item => ({
                value: item.avgReads,
                name: item.novelAuthor,
                allNovelsStr: item.allNovelsStr
            }));

            // 设置ECharts选项
            const option = {
                title: {
                    text: '小说作者平均阅读量TOP10',
                    left: 'center'
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow'
                    },
                },
                xAxis: {
                    type: 'category',
                    data: seriesData.map(item => item.name)
                },
                yAxis: {
                    type: 'value'
                },
                series: [{
                    name: '阅读量',
                    data: seriesData.map(item => item.value),
                    type: 'bar',
                    itemStyle: {
                        color: '#5793f3'
                    }
                }]
            };

            // 使用配置项和数据显示图表
            chart.setOption(option);

            // 监听点击事件
            chart.on('click', function (params) {
                // 查找点击的数据项
                const selectedData = seriesData.find(item => item.name === params.name);
                if (selectedData) {
                    // 显示模态窗口
                    showModal(selectedData.allNovelsStr);
                }
            });
        })
        .catch(error => {
            console.error('Error fetching data:', error);
        });

    // 显示模态窗口
    function showModal(content) {
        var modal = document.getElementById('myModal');
        var modalContent = document.getElementById('modalContent');
        modalContent.textContent = content;
        modal.style.display = 'block';
    }

    // 关闭模态窗口
    function closeModal() {
        var modal = document.getElementById('myModal');
        modal.style.display = 'none';
    }
</script>
</body>
</html>
